<template>
  <div class="dreamcore-login-wrap" ref="loginWrap">
    <!-- 梦核背景 -->
    <div class="dreamcore-bg">
      <div class="vhs-overlay"></div>
      <div class="static-noise"></div>
      <div class="color-bands">
        <div class="band band-1"></div>
        <div class="band band-2"></div>
        <div class="band band-3"></div>
      </div>
      <div class="dream-elements">
        <div class="dream-circle circle-1"></div>
        <div class="dream-circle circle-2"></div>
        <div class="dream-circle circle-3"></div>
        <div class="dream-circle circle-4"></div>
        <div class="dream-shape shape-1">◆</div>
        <div class="dream-shape shape-2">▲</div>
        <div class="dream-shape shape-3">●</div>
      </div>
      <div class="glitch-text">
        <span>D R E A M C O R E</span>
      </div>
    </div>

    <div class="login-container">
      <div class="login-panel">
        <!-- CRT显示器效果 -->
        <div class="crt-frame">
          <div class="scanline"></div>
          <div class="panel-content">
            <div class="panel-header">
              <div class="logo-section">
                <div class="dreamcore-logo">
                  <div class="logo-core"></div>
                  <div class="logo-rings">
                    <div class="ring ring-1"></div>
                    <div class="ring ring-2"></div>
                    <div class="ring ring-3"></div>
                  </div>
                  <div class="logo-glitch"></div>
                </div>
                <h2 class="dreamcore-title">
                  <span class="title-text">ERP</span>
                  <span class="title-subtitle">管理系统</span>
                </h2>
                <p class="dreamcore-subtitle">進入夢境空間</p>
              </div>
            </div>

            <el-form
              ref="loginFormRef"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
              @submit.prevent="handleLogin"
            >
              <el-form-item prop="username">
                <div class="input-wrapper">
                  <div class="input-label">用戶名</div>
                  <el-input
                    v-model="loginForm.username"
                    placeholder="username"
                    size="large"
                    prefix-icon="User"
                    class="dreamcore-input"
                  />
                  <div class="input-underscore"></div>
                </div>
              </el-form-item>

              <el-form-item prop="password">
                <div class="input-wrapper">
                  <div class="input-label">密碼</div>
                  <el-input
                    v-model="loginForm.password"
                    type="password"
                    placeholder="password"
                    size="large"
                    prefix-icon="Lock"
                    class="dreamcore-input"
                    show-password
                  />
                  <div class="input-underscore"></div>
                </div>
              </el-form-item>

              <el-form-item>
                <div class="remember-forgot">
                  <el-checkbox v-model="rememberMe" class="dreamcore-checkbox">
                    <span>記住我</span>
                  </el-checkbox>
                  <a class="dreamcore-link" @click="forgotPassword">
                    <span>忘記密碼?</span>
                  </a>
                </div>
              </el-form-item>

              <el-form-item>
                <el-button
                  type="primary"
                  size="large"
                  class="dreamcore-button"
                  :loading="loading"
                  @click="handleLogin"
                >
                  <span class="button-text">
                    <span>登 錄</span>
                    <span class="button-cursor">█</span>
                  </span>
                </el-button>
              </el-form-item>
            </el-form>

            <div class="panel-footer">
              <p class="footer-text">
                沒有賬號?
                <a class="dreamcore-link" @click="register">
                  <span>立即註冊</span>
                </a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const store = useUserStore()

const emit = defineEmits(['login'])

const loginWrap = ref(null)
const loginFormRef = ref(null)
const loading = ref(false)
const rememberMe = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    const response = await store.login(loginForm)

    if (response.code === 200) {
      ElMessage.success(response.msg || '登录成功')
      emit('login', response)
      router.push('/dashboard')
    } else {
      ElMessage.error(response.msg || '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '网络请求失败，请重试')
  } finally {
    loading.value = false
  }
}

const register = () => {
  ElMessage.info('注册功能开发中...')
}

const forgotPassword = () => {
  ElMessage.info('忘记密码功能开发中...')
}

let cursorBlink = true

onMounted(() => {
  // 光标闪烁效果
  const interval = setInterval(() => {
    cursorBlink = !cursorBlink
    const cursor = document.querySelector('.button-cursor')
    if (cursor) {
      cursor.style.opacity = cursorBlink ? '1' : '0'
    }
  }, 530)
  
  onUnmounted(() => {
    clearInterval(interval)
  })
})
</script>

<style scoped>
.dreamcore-login-wrap {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: #000;
  font-family: 'Courier New', monospace;
}

/* 梦核背景 */
.dreamcore-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  background: radial-gradient(circle at center, #1a0d2e 0%, #000000 100%);
}

/* VHS叠加层 */
.vhs-overlay {
  position: absolute;
  width: 100%;
  height: 100%;
  background: 
    repeating-linear-gradient(
      0deg,
      rgba(255, 0, 255, 0.03),
      rgba(0, 255, 255, 0.03) 2px,
      transparent 2px,
      transparent 4px
    );
  animation: vhsMove 0.1s linear infinite;
  pointer-events: none;
}

@keyframes vhsMove {
  0% { transform: translateY(0); }
  100% { transform: translateY(4px); }
}

/* 静态噪点 */
.static-noise {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 400 400' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='4' numOctaves='5' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)' opacity='0.05'/%3E%3C/svg%3E");
  animation: staticMove 0.2s linear infinite;
  pointer-events: none;
}

@keyframes staticMove {
  0% { transform: translate(0, 0); }
  100% { transform: translate(10px, 10px); }
}

/* 色带 */
.color-bands {
  position: absolute;
  width: 100%;
  height: 100%;
}

.band {
  position: absolute;
  width: 100%;
  height: 20px;
  opacity: 0.1;
  animation: colorShift 3s ease-in-out infinite;
}

.band-1 {
  top: 20%;
  background: linear-gradient(90deg, 
    transparent, 
    #ff00ff, 
    transparent
  );
  animation-delay: 0s;
}

.band-2 {
  top: 50%;
  background: linear-gradient(90deg, 
    transparent, 
    #00ffff, 
    transparent
  );
  animation-delay: 1s;
}

.band-3 {
  top: 80%;
  background: linear-gradient(90deg, 
    transparent, 
    #ffff00, 
    transparent
  );
  animation-delay: 2s;
}

@keyframes colorShift {
  0%, 100% { opacity: 0.1; transform: translateX(0); }
  50% { opacity: 0.3; transform: translateX(10px); }
}

/* 梦核元素 */
.dream-elements {
  position: absolute;
  width: 100%;
  height: 100%;
}

.dream-circle {
  position: absolute;
  border-radius: 50%;
  border: 2px solid;
  animation: dreamFloat 8s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 5%;
  border-color: #ff00ff;
  box-shadow: 0 0 30px #ff00ff;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  bottom: 20%;
  left: 10%;
  border-color: #00ffff;
  box-shadow: 0 0 30px #00ffff;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  top: 50%;
  right: 15%;
  border-color: #ffff00;
  box-shadow: 0 0 30px #ffff00;
  animation-delay: 4s;
}

.circle-4 {
  width: 80px;
  height: 80px;
  bottom: 10%;
  right: 5%;
  border-color: #ff00ff;
  box-shadow: 0 0 20px #ff00ff;
  animation-delay: 6s;
}

@keyframes dreamFloat {
  0%, 100% { transform: translate(0, 0) scale(1); opacity: 0.5; }
  50% { transform: translate(30px, -30px) scale(1.2); opacity: 1; }
}

.dream-shape {
  position: absolute;
  font-size: 40px;
  color: #00ffff;
  text-shadow: 0 0 20px #00ffff;
  animation: shapeRotate 10s linear infinite;
}

.shape-1 {
  top: 20%;
  right: 20%;
  animation-delay: 0s;
}

.shape-2 {
  top: 60%;
  right: 30%;
  animation-delay: 3s;
  font-size: 30px;
}

.shape-3 {
  bottom: 15%;
  left: 20%;
  animation-delay: 6s;
  font-size: 25px;
}

@keyframes shapeRotate {
  0% { transform: rotate(0deg); opacity: 0.3; }
  50% { opacity: 1; }
  100% { transform: rotate(360deg); opacity: 0.3; }
}

.glitch-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 60px;
  font-weight: bold;
  color: transparent;
  -webkit-text-stroke: 1px rgba(255, 255, 255, 0.1);
  letter-spacing: 20px;
  animation: glitchMove 5s ease-in-out infinite;
  pointer-events: none;
}

@keyframes glitchMove {
  0%, 100% { transform: translate(-50%, -50%) translate(0); }
  25% { transform: translate(-50%, -50%) translate(-3px, 3px); }
  50% { transform: translate(-50%, -50%) translate(3px, -3px); }
  75% { transform: translate(-50%, -50%) translate(-2px, -2px); }
}

/* 登录容器 */
.login-container {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.login-panel {
  position: relative;
  width: 480px;
}

/* CRT显示器框架 */
.crt-frame {
  position: relative;
  background: linear-gradient(
    135deg,
    rgba(0, 0, 0, 0.9),
    rgba(20, 0, 40, 0.9)
  );
  border: 4px solid #333;
  box-shadow: 
    0 0 40px rgba(0, 0, 0, 0.8),
    inset 0 0 40px rgba(0, 255, 255, 0.05);
  overflow: hidden;
}

.crt-frame::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      rgba(0, 255, 255, 0.03) 2px,
      rgba(0, 255, 255, 0.03) 4px
    );
  pointer-events: none;
  z-index: 2;
}

.scanline {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(
    to bottom,
    rgba(0, 255, 255, 0.5),
    transparent
  );
  animation: scanlineMove 8s linear infinite;
  pointer-events: none;
  z-index: 3;
}

@keyframes scanlineMove {
  0% { top: 0; opacity: 1; }
  98% { opacity: 1; }
  100% { top: 100%; opacity: 0; }
}

.panel-content {
  position: relative;
  padding: 50px 40px;
  z-index: 1;
}

.panel-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo-section {
  position: relative;
}

.dreamcore-logo {
  position: relative;
  width: 120px;
  height: 120px;
  margin: 0 auto 25px;
}

.logo-core {
  position: absolute;
  width: 60px;
  height: 60px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 3px solid #ff00ff;
  box-shadow: 
    0 0 20px #ff00ff,
    inset 0 0 20px #ff00ff;
  animation: corePulse 2s ease-in-out infinite;
  z-index: 2;
}

@keyframes corePulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  50% { transform: translate(-50%, -50%) scale(1.1); opacity: 0.8; }
}

.logo-rings {
  position: absolute;
  width: 100%;
  height: 100%;
}

.ring {
  position: absolute;
  top: 50%;
  left: 50%;
  border: 2px solid;
  border-radius: 50%;
  animation: ringExpand 3s ease-out infinite;
}

.ring-1 {
  width: 80px;
  height: 80px;
  margin-left: -40px;
  margin-top: -40px;
  border-color: #00ffff;
  animation-delay: 0s;
}

.ring-2 {
  width: 100px;
  height: 100px;
  margin-left: -50px;
  margin-top: -50px;
  border-color: #ff00ff;
  animation-delay: 0.5s;
}

.ring-3 {
  width: 120px;
  height: 120px;
  margin-left: -60px;
  margin-top: -60px;
  border-color: #ffff00;
  animation-delay: 1s;
}

@keyframes ringExpand {
  0% { transform: scale(0.8); opacity: 1; }
  100% { transform: scale(1.5); opacity: 0; }
}

.logo-glitch {
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    45deg,
    transparent 30%,
    rgba(255, 255, 255, 0.1) 50%,
    transparent 70%
  );
  animation: glitchShine 3s linear infinite;
}

@keyframes glitchShine {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

.dreamcore-title {
  font-size: 36px;
  font-weight: bold;
  margin: 0 0 10px;
}

.title-text {
  display: inline-block;
  color: #00ffff;
  text-shadow: 
    0 0 10px #00ffff,
    0 0 20px #00ffff,
    0 0 30px #00ffff;
  animation: textFlicker 3s ease-in-out infinite;
}

@keyframes textFlicker {
  0%, 90%, 100% { opacity: 1; }
  91% { opacity: 0.8; }
  93% { opacity: 0.9; }
  95% { opacity: 0.8; }
  97% { opacity: 1; }
}

.title-subtitle {
  display: block;
  font-size: 20px;
  color: #ff00ff;
  text-shadow: 0 0 15px rgba(255, 0, 255, 0.5);
  margin-top: 5px;
}

.dreamcore-subtitle {
  font-size: 14px;
  color: #ffff00;
  margin: 0;
  letter-spacing: 5px;
  text-shadow: 0 0 10px rgba(255, 255, 0, 0.5);
}

/* 登录表单 */
.login-form {
  width: 100%;
}

.input-wrapper {
  position: relative;
  margin-bottom: 30px;
}

.input-label {
  color: #00ffff;
  font-size: 12px;
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 2px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
}

.dreamcore-input {
  width: 100%;
}

:deep(.el-input__wrapper) {
  background: rgba(0, 0, 0, 0.6);
  border: 2px solid rgba(0, 255, 255, 0.3);
  box-shadow: none;
  transition: all 0.3s;
  border-radius: 0;
}

:deep(.el-input__wrapper:hover) {
  border-color: rgba(0, 255, 255, 0.6);
  box-shadow: 0 0 15px rgba(0, 255, 255, 0.3);
}

:deep(.el-input.is-focus .el-input__wrapper) {
  border-color: #00ffff;
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.5);
  background: rgba(0, 255, 255, 0.1);
}

:deep(.el-input__inner) {
  color: #00ffff;
  background: transparent;
}

:deep(.el-input__inner::placeholder) {
  color: rgba(0, 255, 255, 0.4);
}

.input-underscore {
  position: absolute;
  bottom: -3px;
  left: 0;
  width: 0;
  height: 2px;
  background: #00ffff;
  box-shadow: 0 0 5px #00ffff;
  transition: width 0.3s;
}

:deep(.el-input.is-focus ~ .input-underscore) {
  width: 100%;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.dreamcore-checkbox :deep(.el-checkbox__label) {
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

.dreamcore-checkbox :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #00ffff;
  border-color: #00ffff;
  box-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
}

.dreamcore-link {
  color: #ff00ff;
  font-size: 14px;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.3s;
  position: relative;
}

.dreamcore-link span {
  position: relative;
  z-index: 2;
}

.dreamcore-link::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 1px;
  background: #ff00ff;
  box-shadow: 0 0 5px #ff00ff;
  transition: width 0.3s;
}

.dreamcore-link:hover::after {
  width: 100%;
}

.dreamcore-link:hover {
  text-shadow: 0 0 10px #ff00ff;
}

/* 登录按钮 */
.dreamcore-button {
  width: 100%;
  height: 50px;
  background: linear-gradient(135deg, rgba(0, 255, 255, 0.2), rgba(255, 0, 255, 0.2));
  border: 2px solid #00ffff;
  color: #00ffff;
  font-size: 18px;
  font-weight: bold;
  letter-spacing: 3px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s;
  box-shadow: 
    0 0 20px rgba(0, 255, 255, 0.3),
    inset 0 0 20px rgba(0, 255, 255, 0.1);
}

.dreamcore-button:hover {
  background: linear-gradient(135deg, rgba(0, 255, 255, 0.4), rgba(255, 0, 255, 0.4));
  box-shadow: 
    0 0 30px rgba(0, 255, 255, 0.6),
    inset 0 0 30px rgba(0, 255, 255, 0.2);
  transform: translateY(-2px);
}

.button-text {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 5px;
}

.button-cursor {
  animation: cursorBlink 1s infinite;
}

@keyframes cursorBlink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

:deep(.dreamcore-button.is-loading) {
  pointer-events: none;
}

.panel-footer {
  text-align: center;
  margin-top: 30px;
}

.footer-text {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}
</style>

